(function($) {

	$.fn.isAddOrRemoveFollow = function(options){

		var defaults = {
				followFormCommonClass: 'follow_form',		//关注表单的class(加关注与取消关注一致)
				addFollowFormId: 'add_follow',				//加关注form的id
				removeFollowFormId: 'remove_follow',		//取消关注form的id
				
				followBtnCommonClass: 'follow_action_btn',	//加关注和取消关注按钮相同的class
				addFollowBtnClass: 'follow-btn',			//加关注按钮的class
				removeFollowBtnClass: 'cancel-follow-btn',	//取消关注按钮的class
				
				changeFansNum: false,						//是否改变被关注者粉丝人数
				fansNumParentsClass: '',					//粉丝人数与加关注的共同父节点的class
				fansNumType: 'span',						//包围粉丝人数元素的标签
				fansNumClass: 'fans-num',					//包围粉丝人数元素的class
				
				onAddComplete: function(){},				//提供关注后除了人数变化以外的复杂操作的回调方法
				onRemoveComplete: function(){},				//提供取消关注后除了人数变化以外的复杂操作的回调方法
				onComplete: function(){},					//如果加关注和取消关注的回调方法一样，则用此回调方法
				onAnonymous: function(){					//匿名点击时的回调方法
					$.error("You need login to do the follow action.");
					return false;
				},
				onException: function(){					//异常处理的回调方法，一般就是404时的处理
					$.error("Something is wrong. Try again, please.");
					return false;
				}
			},
			
			settings = $.extend({}, defaults, options),
			
			followForm = $('.' + settings.followFormCommonClass, this),
			
			that = $(this);

		return $(this).each(function(){
			
			$(followForm).each(function(){
				
				$(this).submit(function(){
										
					var fansNumParents = settings.fansNumParentsClass === '' ? $(that) : $(this).parents('.'+ settings.fansNumParentsClass);
						addFollowForm = $(this).parent().find('#' + settings.addFollowFormId),
						removeFollowForm = $(this).parent().find('#' + settings.removeFollowFormId),
						follow_action_btn = $('.' + settings.followBtnCommonClass, this);
					
					$.ajax({
						async:false,
						url:$(this).attr('action'),
						dataType:'json',
						type:'POST',
						data:$(this).serialize(),
						beforeSend: function (xhr) {
							 xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
						},
						statusCode: {
							405: function() {
								settings.onException();
							},
							404: function() {
								settings.onException();
							},
							403: function() {
								settings.onAnonymous();
							}
						},
						success:function(json){
							
							if(json.success){
								
								if(follow_action_btn.hasClass(settings.addFollowBtnClass)){
									
									$(addFollowForm).hide();
									$(removeFollowForm).fadeIn('slow');
								
									if(settings.changeFansNum){
										
										if($('' + settings.fansNumType + '.' + settings.fansNumClass, fansNumParents)[0]){
											
											var fansNumber = $('' + settings.fansNumType + '.' + settings.fansNumClass, fansNumParents);
											
											fansNumber.each(function(index){
											
												$(this).text(parseInt($(this).text()) + 1);
											});
										}
									}
									
									settings.onAddComplete();
									
								}else if(follow_action_btn.hasClass(settings.removeFollowBtnClass)){
								
									$(removeFollowForm).hide();
									$(addFollowForm).fadeIn('slow');
								
									if(settings.changeFansNum){
										
										if($('' + settings.fansNumType + '.' + settings.fansNumClass, fansNumParents)[0]){
											
											var fansNumber = $('' + settings.fansNumType + '.' + settings.fansNumClass, fansNumParents);
											
											fansNumber.each(function(index){
											
												$(this).text(parseInt($(this).text()) - 1);
											});
										}
									}
									
									settings.onRemoveComplete();
								}
							}
							else {
								return false;
							}
							
							settings.onComplete();
						}
					});
					
					return false;
				});
			});
		});
	};
})(jQuery);
